/********************************************************************
 *  Proyecto: Sistema modular de comunicacion con un robot movil
 *  Subproyecto: Servidor
 *  Archivo: modcom-client.c
 * 	Version: 0.1
 *
 *  Autor: Manuel Angel Abeledo Garcia
 ********************************************************************/

#include <gtk/gtk.h>
#include <glade/glade.h>

#include "base.h"
#include "handler.h"

#define MODCOM_CLIENT_GLADE		"modcom-client.glade"
#define MODCOM_CFG				"modcom.cfg"
#define WIDGET_HANDLERS			2


/* Funcion main
 * Precondiciones:
 * Postcondiciones:
 * */
gint
main						(gint argc, gchar **argv)
{
	GladeXML* uiDefs;
	GtkWindow* window;
	GtkStatusbar* mainStatusbar;
	
	gchar* returnError = "";
	gchar* optConfig = MODCOM_CFG;
	
	/* Initialize gtk+. */
	gtk_init(&argc, &argv);
	
	/* Load glade file. */
	uiDefs = glade_xml_new(MODCOM_CLIENT_GLADE, NULL, NULL);
	window = GTK_WINDOW(glade_xml_get_widget(uiDefs, "mainWindow"));
	
	/* Attach signal handlers. */
	glade_xml_signal_connect_data(uiDefs, "on_commandButton_clicked", G_CALLBACK(on_commandButton_clicked), (gpointer)uiDefs);
	
	/* Menu handlers. */
	glade_xml_signal_connect_data(uiDefs, "on_saveImgMenuItem_activate", G_CALLBACK(on_saveImgMenuItem_activate), GUINT_TO_POINTER(uiDefs));
	glade_xml_signal_connect_data(uiDefs, "on_quitImgMenuItem_activate", G_CALLBACK(on_quitImgMenuItem_activate), GUINT_TO_POINTER(uiDefs));
	glade_xml_signal_connect_data(uiDefs, "on_aboutImgMenuItem_activate", G_CALLBACK(on_aboutImgMenuItem_activate), GUINT_TO_POINTER(uiDefs));
	
	/* Attach standard handlers. */
	g_signal_connect(window, "delete_event", G_CALLBACK(delete_event), NULL);
	g_signal_connect(window, "destroy", G_CALLBACK(end_program), NULL);
	
	/* All handlers loaded, so push a message into the statusbar. */
	mainStatusbar = GTK_STATUSBAR(glade_xml_get_widget(uiDefs, "mainStatusbar"));
	gtk_statusbar_push(mainStatusbar, 0, "Modcom client up & running");
	
	/* Initialize low level modules. */
	if (!initBaseSystem(optConfig, &returnError))
	{
		g_critical("%s", returnError);
		gtk_statusbar_push(mainStatusbar, 0, "Error loading low level modules. Client is unusable.");
	}

	/* 
	 * Creating a thread here? Well, it could be worst...
	 * */
	g_thread_create(&loadResults, (gpointer)uiDefs, FALSE, NULL);
	
	/* Run the program. */
	gtk_main();
	
	return (0);
}
